package com.itheima.utils;

import com.itheima.dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class DaoFactory<T> {
    private static SqlSessionFactory factory;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回dao代理实现类对象
     *
     * @return
     */
    public static <T> T getDaoInstance(Class daoClass) {
        SqlSession sqlSession = factory.openSession(true);
        return (T) sqlSession.getMapper(daoClass);
    }
}
